package com.cloudfast.platform.dict.controller;

import com.cloudfast.platform.dict.entity.DictType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.controller.BaseController;
import com.cloudfast.platform.dict.entity.Dict;
import com.cloudfast.platform.dict.service.DictService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author liuyw
 * @描述:字典表
 * @date 2020年4月10日
 */
@RestController
@RequestMapping(value = "dict")
@Api(value = "字典管理", tags = "字典管理")
public class DictController extends BaseController<Dict> {

    @Autowired
    private DictService dictService;

    /**
     * @param value
     * @param dictType
     * @return
     * @描述:查询字典项名称
     * @author liuyw
     * @date 2020年4月10日
     */
    @PostMapping(value = "findLabel")
    @ApiOperation("根据字典类型和字典项查询字典标签")
    @ApiImplicitParams({@ApiImplicitParam(name = "value", value = "字典项", required = true),
            @ApiImplicitParam(name = "dictType", value = "字典类型", required = true),})
    public ApiResponse findLabel(String value, String dictType) {
        return ApiResponse.success(this.dictService.findByDictTypeAndValue(dictType, value));
    }

    /**
     * 字典类型查询
     *
     * @return
     */
    @PostMapping(value = "findDictTypeList")
    @ApiOperation("查询字典分类")
    public ApiResponse findDictTypeList() {
        return ApiResponse.success(this.dictService.findDictTypeList(new DictType()));
    }

    /**
     * 字典类型查询
     *
     * @return
     */
    @PostMapping(value = "saveDictType")
    @ApiOperation("保存字典类型")
    public ApiResponse saveDictType(DictType dictType) {
        try {
            this.dictService.saveDictType(dictType);
            return ApiResponse.success(true);
        } catch (Exception e) {
            return ApiResponse.error("操作异常");
        }
    }

    @PostMapping(value = "deleteDictType")
    @ApiOperation("删除字典类型")
    public ApiResponse deleteDictType(Long id) {
        try {
            this.dictService.deleteDictType(id);
            return ApiResponse.success(true);
        } catch (Exception e) {
            return ApiResponse.error("删除异常");
        }
    }

    /**
     * 根据类型查询字典信息
     *
     * @param dictType
     * @return
     */
    @ApiOperation("根据字典类型查询字典项")
    @ApiImplicitParams({@ApiImplicitParam(name = "dictType", value = "字典类型", required = true),})
    @PostMapping(value = "findType")
    public ApiResponse findType(String dictType) {
        return ApiResponse.success(this.dictService.findListByDictType(dictType));
    }

}
